<?php

namespace App\Admin\Controllers;
use Illuminate\Http\Request;
use App\crm_funds;
use App\crm_order;
use App\crm_deal;
use App\crm_supplier;
use App\supplier_type;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_shenpiController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('请款管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }
    public function update(Request $request,$id)
    {
        $list=$request->except("_token");
        if($list['value']==1){
            $receive=crm_funds::find($list['pk']);
            if($receive->funds_statue==2){
                $deal=crm_deal::find($receive->funds_dealid);
                $deal->deal_sdakuan-=$receive->funds_money;
                $deal->save();
                $receive->funds_statue=$list['value'];
                $receive->funds_xiaoxi=1;
                $receive->save();
            }else{
                $receive->funds_statue=$list['value'];
                $receive->funds_xiaoxi=1;
                $receive->save();
            }
            
        }elseif($list['value']==2){
            $receive=crm_funds::find($list['pk']);
            $receive->funds_statue=$list['value'];
            $receive->funds_xiaoxi=2;
            $receive->save();
            $deal=crm_deal::find($receive->funds_dealid);
            $deal->deal_sdakuan+=$receive->funds_money;
            $deal->deal_lirun=$deal->deal_sshoukuan-$deal->deal_sdakuan;
            $deal->save();
        }elseif($list['value']==3){
            $receive=crm_funds::find($list['pk']);
            if($receive->funds_statue==2){
                $deal=crm_deal::find($receive->funds_dealid);
                $deal->deal_sdakuan-=$receive->funds_money;
                $deal->save();
                $receive->funds_statue=$list['value'];
                $receive->funds_xiaoxi=2;
                $receive->save();
            }else{
                $receive->funds_statue=$list['value'];
                $receive->funds_xiaoxi=2;
                $receive->save();
            }
            
        }
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_funds::class, function (Grid $grid) {
            $grid->model()->where('funds_dianzi', 2)->orderBy('id', 'desc');
            $grid->column("客户")->display(function () {
                return crm_order::find(crm_deal::find($this->funds_dealid)->deal_orderid)->crm_customer->customer_name;
            });
            $grid->column("电话")->display(function () {
                return crm_order::find(crm_deal::find($this->funds_dealid)->deal_orderid)->crm_customer->customer_phone;
            });
            $grid->column("线路名称")->display(function () {
                return crm_order::find(crm_deal::find($this->funds_dealid)->deal_orderid)->order_xianlu;
            });
            $grid->column("发团日期")->display(function () {
                return crm_deal::find($this->funds_dealid)->deal_ftdate;
            });
            $grid->funds_name("请款人姓名");
            $grid->funds_money("请款金额");
            $grid->funds_date("请款时间");
            $grid->funds_statue('请款状态')->editable('select', ['1'=>'垫资审批中','2'=>'通过','3'=>'驳回']);
            $grid->disableCreation();//禁用创建按钮
            // $grid->disableFilter();//禁用查询过滤器
            $grid->disableExport();//禁用导出按钮
            $grid->disableRowSelector();//禁用行选择
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 收款状态
                $filter->equal('funds_name','请款人');
                $filter->equal('funds_statue','请款状态')->select(['1'=>'审批中','2'=>'通过','3'=>'驳回']);
                // 收款日期
                $filter->between('funds_date', '请款日期')->datetime();
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();//禁用删除按钮
                $actions->disableEdit();//禁用编辑按钮
                $actions->append('
                    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#qkModal">查看详情</button>
                    <div class="modal fade" id="qkModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">请款详情</h4>
                          </div>
                          <div class="modal-body">
                                <div class="form">
                                    <div class="form-group">
                                    请款人：<input type="text" value="'.$actions->row->funds_name.'">
                                    </div>
                                    <div class="form-group">
                                    供应商名称：<input type="text" value="'.crm_supplier::where('id',$actions->row->funds_gongyingshang)->first()->supplier_name.'">
                                    </div>
                                    <div class="form-group">
                                    请款方式：<input type="text" value="'.supplier_type::where('id',$actions->row->funds_zhtype)->first()->typename.'">
                                    </div>
                                    <div class="form-group">
                                    请款账号：<input type="text" value="'.$actions->row->funds_zhanghu.'">
                                    </div>
                                    <div class="form-group">
                                    请款备注：<textarea name="beizhu" id="qk" style="resize:none;width:100%;margin:0 auto;height:200px;">'.$actions->row->funds_beizhu.'</textarea>
                                    </div>
                                </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    ');  
                
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_funds::class, function (Form $form) {

            $form->display('id', 'ID');
            $zhuangtai=['1'=>'审批中','2'=>'通过','3'=>'驳回'];
            $form->select('funds_statue','状态')->options($zhuangtai);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
